package com.kaiwukj.android.ufamily.mvp.ui.page.mine.evaluate;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.mcas.http.imageloader.glide.GlideArms;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderEvaluateResult;
import com.kaiwukj.android.ufamily.mvp.ui.widget.order.CustomRatingBar;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class OrderEvaluateAdapter extends BaseQuickAdapter<OrderEvaluateResult, BaseViewHolder> {
    private int type = 0;

    public OrderEvaluateAdapter(int type) {
        super(R.layout.item_order_evaluate, new ArrayList<>());
        this.type = type;
        addChildClickViewIds(R.id.btn_rating);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    public void removeItemById(long id) {
        for (int i = getData().size() - 1; i >= 0; i--) {
            if (getData().get(i).getId() == id) {
                removeAt(i);
            }
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, OrderEvaluateResult result) {
        ImageView ivImage = holder.getView(R.id.iv_image);
        GlideArms.with(getContext())
                .load(result.getThumbnail())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(ivImage);

        holder.setText(R.id.tv_name, result.getServiceName());
        holder.setText(R.id.tv_desc, result.getServiceParameterName());
        holder.setText(R.id.tv_price, String.format("¥%s", result.getActualprice() / result.getNum()));
        holder.setText(R.id.tv_num, String.format("x%s", result.getNum()));

        TextView tvRatingFlag = holder.getView(R.id.tv_rating_flag);
        CustomRatingBar ratingBar = holder.getView(R.id.rating_bar);
        Button btnRating = holder.getView(R.id.btn_rating);

        if (type == 0) {
            tvRatingFlag.setVisibility(View.GONE);
            ratingBar.setVisibility(View.GONE);
            btnRating.setVisibility(View.VISIBLE);
        } else {
            tvRatingFlag.setVisibility(View.VISIBLE);
            ratingBar.setVisibility(View.VISIBLE);
            ratingBar.setStar(result.getServiceRating());
            btnRating.setVisibility(View.GONE);
        }

    }
}
